import Vue from "vue"
import VueRouter from "vue-router"
// 导入需要的组件
import Login from "@/components/MyLogin.vue"
import Home from "@/components/MyHome.vue"

import Users from "@/components/menus/MyUsers.vue"
import Rights from "@/components/menus/MyRights.vue"
import Goods from "@/components/menus/MyGoods.vue"
import Orders from "@/components/menus/MyOrders.vue"
import Settings from "@/components/menus/MySettings.vue"

import Deta from "@/components/user/MyUserDetail.vue"

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    // 重定向路由规则
    {path:'/',redirect:'/login'},
    // 登录的路由规则
    { path: '/login', component: Login },
    {
      path: '/home',redirect:'/home/users', component: Home, children: [
      {path:'users',component:Users},
      {path:'rights',component:Rights},
      {path:'goods',component:Goods},
      {path:'orders',component:Orders},
        { path: 'settings', component: Settings },
      // 用户详情页路由规则
        {path:'ifo/:id',component:Deta, props:true}
    ]},
  ]
})
// 路由的全局前置守卫
router.beforeEach(function (to, from, next) {
  const arr = ['/home','/home/users', '/home/rights']
  if (arr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router